﻿Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class ProgressBar
    Inherits System.Windows.Forms.ProgressBar


    Private m_CustomText As String = String.Empty
    ''' <summary>
    ''' 在进度条中显示的文字
    ''' </summary>
    <Category("在进度条中显示的文字")> _
    Public Property CustomText() As String
        Get
            Return m_CustomText
        End Get
        Set(ByVal value As String)
            m_CustomText = value
        End Set
    End Property


    Private m_CustomFormat As String = "{0}% {1}"
    ''' <summary>
    ''' 自定义文字格式
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <DefaultValue("{0}% {1}")> _
    Public Property CustomFormat() As String
        Get
            Return m_CustomFormat
        End Get
        Set(ByVal value As String)
            m_CustomFormat = value
        End Set
    End Property



    Private m_CustomFont As Font = New Font("Cambria", 12.0!, FontStyle.Regular, GraphicsUnit.Pixel)
    ''' <summary>
    ''' 
    ''' </summary>
    <Category("进度条中文字设置")> _
    Public Property CustomFont() As Font
        Get
            Return m_CustomFont
        End Get
        Set(ByVal value As Font)
            m_CustomFont = value
        End Set
    End Property


    Private m_CustomFontColor As Color = Color.FromArgb(255, 255, 0, 0)
    ''' <summary>
    ''' 进度条文字的颜色
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Category("进度条文字的颜色")> _
    Public Property CustomFontColor() As Color
        Get
            Return m_CustomFontColor
        End Get
        Set(ByVal value As Color)
            m_CustomFontColor = value
        End Set
    End Property


    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const WM_PAINT As Integer = &HF
        MyBase.WndProc(m)
        Select Case m.Msg
            Case WM_PAINT
                Dim _ProgressValue As String = String.Format(Me.m_CustomFormat, CStr(Me.Value), Me.m_CustomText)
                If Me.Style = ProgressBarStyle.Marquee Then
                    _ProgressValue = Me.m_CustomText
                End If

                'Dim _Graphics As Graphics = Me.CreateGraphics()
                'Dim _FontSizeF As SizeF = _Graphics.MeasureString(_ProgressValue, Me.CustomFont)
                'Dim _FontPointF As PointF = Nothing
                'If _FontSizeF.Width > Me.Width Then
                '    _FontPointF = New PointF(0, CSng(Me.Height - _FontSizeF.Height) / 2)
                'Else
                '    _FontPointF = New PointF(CSng(Me.Width - _FontSizeF.Width) / 2, CSng(Me.Height - _FontSizeF.Height) / 2)
                'End If
                'Dim _FontBrush As New SolidBrush(Me.m_CustomFontColor)

                '_Graphics.DrawString(_ProgressValue, Me.CustomFont, _FontBrush, _FontPointF)

                Dim _Graphics As Graphics = Me.CreateGraphics()
                Dim _Rectangle As New Rectangle(Point.Empty, Me.Size)
                TextRenderer.DrawText(_Graphics, _ProgressValue, Me.CustomFont, _Rectangle, Color.Red, _
                                      TextFormatFlags.VerticalCenter Or _
                                      TextFormatFlags.HorizontalCenter Or _
                                      TextFormatFlags.SingleLine Or _
                                      TextFormatFlags.EndEllipsis)

                Exit Select
        End Select

    End Sub


End Class
